package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dialog.UpdateDialogFragment;
import com.poisonh.unifiedauth.service.DownLoadService;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 关于
 * Created by PoisonH on 2016/7/7.
 */
public class AboutActivity extends BaseActivity implements View.OnClickListener, UpdateDialogFragment.UpdateAppListener
{
    private EditText mEtInput;
    private TextView mTvNumTips;
    private Button mBtnCommit;
    private AutoRelativeLayout mArlAboutA;
    private AutoRelativeLayout mArlAboutB;
    private AutoRelativeLayout mArlAboutC;
    private AutoRelativeLayout mArlAboutD;
    private String mStrContent;
    private String mStrDownLoadUrl;


    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_about);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        setTitleTxt("关于");
        mRightBtn.setVisibility(View.GONE);
        mEtInput = (EditText) findViewById(R.id.et_input);
        mBtnCommit = (Button) findViewById(R.id.btn_commit);
        mArlAboutA = (AutoRelativeLayout) findViewById(R.id.arl_about_a);
        mArlAboutB = (AutoRelativeLayout) findViewById(R.id.arl_about_b);
        mArlAboutC = (AutoRelativeLayout) findViewById(R.id.arl_about_c);
        mArlAboutD = (AutoRelativeLayout) findViewById(R.id.arl_about_d);
        mTvNumTips = (TextView) findViewById(R.id.tv_num_tips);

        mArlAboutA.setOnClickListener(this);
        mArlAboutB.setOnClickListener(this);
        mArlAboutC.setOnClickListener(this);
        mArlAboutD.setOnClickListener(this);
        mBtnCommit.setOnClickListener(this);
        mEtInput.setOnClickListener(this);
        mLeftBtn.setOnClickListener(this);

        mEtInput.addTextChangedListener(mTextWatcher);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.arl_about_a:
                Intent intent = new Intent(AboutActivity.this, ServiceListActvity.class);
                intent.putExtra("TITLE", "服务条款");
                startActivity(intent);
                break;
            case R.id.arl_about_b:
                startActivity(new Intent(AboutActivity.this, ContactUsActvity.class));
                break;
            case R.id.arl_about_c:
                startActivity(new Intent(AboutActivity.this, SupportedAppActivity.class));
                break;
            case R.id.arl_about_d://软件升级
                updateApp();
                break;
            case R.id.et_input:
                if (mEtInput.isFocused())
                {
                    mEtInput.setCursorVisible(true);
                }
                break;
            case R.id.btn_commit:
                mStrContent = mEtInput.getText().toString();
                if (mStrContent.equals(""))
                {
                    ToastUtils.showToast(this, "请输入你要反馈的问题");
                } else
                {
                    problemFeedback(mStrContent);
                }
                break;
            case R.id.ib_left_btn:
                finish();
                break;

        }
    }

    /**
     * 问题反馈
     */
    private void problemFeedback(String content)
    {
        HttpParams params = new HttpParams();
        PreferencesUtils.getString(this, "ID");
        params.put("id", PreferencesUtils.getString(this, "ID"));
        params.put("token", mStrUserToken);
        params.put("content", content);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.PROBLEMFEEDBACK, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                ToastUtils.showToast(AboutActivity.this, "感谢你宝贵的意见");
                mEtInput.setText("");
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(AboutActivity.this, failReason);
            }
        });
    }

    private void updateApp()
    {
        HttpParams params = new HttpParams();
        params.put("type", "1");
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.THELATESTVERSION, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                try
                {
                    JSONObject mjson = new JSONObject(response);
                    JSONObject mdata = mjson.getJSONObject("results");
                    if (mdata.getString("version").equals(getVersion()))
                    {
                        ToastUtils.showToast(AboutActivity.this, "你已经是最新版本！");
                    } else
                    {
                        mStrDownLoadUrl = ConstantUitls.REQUEDT_URL + mdata.getString("downUrl");
                        UpdateDialogFragment updateDialogFragment = new UpdateDialogFragment();
                        updateDialogFragment.show(getFragmentManager(), "");
                    }
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(AboutActivity.this, failReason);
            }
        });
    }

    //获取当前版本的版本号

    private String getVersion()
    {
        try
        {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);//0代表是获取版本信息
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
            return "版本号未知";
        }
    }


    @Override
    public void onUpdateAppListener(boolean update)
    {
        if (update)
        {
            Intent intent = new Intent(this, DownLoadService.class);
            intent.putExtra("URL", mStrDownLoadUrl);
            this.startService(intent);
        }
    }

    TextWatcher mTextWatcher = new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            int num = s.length();
            mTvNumTips.setText(num + "/200");
        }
    };
}
